
let callBack = null;

let mainCallBack = null;

const setCallBack = function (callBackFunc) {
    callBack = callBackFunc;
};

const exeCallBack = function () {
    callBack && callBack.apply(null, arguments);
	callBack = null;
};

const setMainCallBack = function (mainCallBackFunc) {
    mainCallBack = mainCallBackFunc;
};

const exeMainCallBack = function () {
    mainCallBack && mainCallBack.apply(null, arguments);
};
//地理位置
function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
//参数
function toParam(data) {
	let param = "?", item = [];
	for(let name in data) {
		let value = data[name]; 
		if(typeof data[name] == 'object') {
			value = JSON.stringify(data[name]);
		}
		item.push(`${name}=${value}`);
	}
	param += item.join('&');
	return param;
}

function getCheckList(data, id, checkObj) {
	for (let i = 0; i < data.length; i++) { 
		let item = data[i];
		if (item.id == id) {
			checkObj.checkList.unshift({
				index: [i],
				checkVal: item.name,
				checkId: item.id,
			});
			checkObj.check = {
				checkVal: item.name,
				checkId: item.id,
				index: [i]
			};
			return true;
		} else if (item != null && Array.isArray(item.children) && item.children.length != 0) {
			let isPush = getCheckList(item.children, id, checkObj);
			if (isPush) {
				checkObj.checkList.unshift({
					index: [i],
					checkVal: item.name,
					checkId: item.id,
				});
				return true;
			}
		}
	}
}

function postData(data) {
	let item = [];
	for(let name in data) {
		let value = data[name]; 
		if(typeof data[name] == 'object') {
			value = JSON.stringify(data[name]);
		}
		console.log(encodeURIComponent(name+'='+value));
		item.push(encodeURIComponent(name+'='+value));
	}
	return item.join('&');
}

function formatDate(date,fmt){
    if(/(y+)/.test(fmt)){
        fmt = fmt.replace(RegExp.$1, (date.getFullYear()+'').substr(4-RegExp.$1.length));
    }
    let o = {
        'M+': date.getMonth()+1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
    }
    for(let k in o){    
        let str = o[k]+'';
        if(new RegExp(`(${k})`).test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length===1)?str:padLeftZero(str));
        }
    }
    return fmt;
};


function loadMoreText(index, lenght, pageClass, conClass, callBack){
	//console.log(index, lenght);
	if(index == lenght-1) {
		let heightObj = {};
		setTimeout(()=>{
			let pageView = uni.createSelectorQuery().select(pageClass);
			pageView.fields({
			size: true
			}, data => {
				heightObj['page'] = data.height;
				setLoadMoreText(heightObj, callBack);
			}).exec();
			
			let conView = uni.createSelectorQuery().select(conClass);
			conView.fields({
			size: true
			}, data => {
				heightObj['con'] = data.height;
				setLoadMoreText(heightObj, callBack);
			}).exec();
		}, 100);
	}
}

function setLoadMoreText(heightObj, callBack){
	if(Object.keys(heightObj).length == 2 && heightObj.con < heightObj.page) {
		callBack();
	}
}

function padLeftZero(str){
    return ('00'+str).substr(str.length);
}

// 两数相除
function accDiv(num1,num2){
    var t1, t2, r1, r2;
    try{
        t1 = num1.toString().split('.')[1].length;
    }catch(e){
        t1 = 0;
    }
    try{
        t2=num2.toString().split(".")[1].length;
    }catch(e){
        t2=0;
    }
    r1=Number(num1.toString().replace(".",""));
    r2=Number(num2.toString().replace(".",""));
    return (r1/r2)*Math.pow(10,t2-t1);
}

export default {
    setCallBack,
    exeCallBack,
	formatLocation,
	toParam,
	formatDate,
	loadMoreText,
	postData,
	setMainCallBack,
	exeMainCallBack,
	getCheckList,
	accDiv
}